package edu.xhu.behavior.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.xhu.api.id.IdClient;
import edu.xhu.behavior.mapper.BiliUserFollowGroupMapper;
import edu.xhu.behavior.mapper.BiliUserFollowMapper;
import edu.xhu.behavior.service.BiliUserFollowService;
import edu.xhu.behavior.util.IdGen;
import edu.xhu.model.behavior.enums.FollowType;
import edu.xhu.model.behavior.pojos.BiliUserFollow;
import edu.xhu.model.behavior.pojos.BiliUserFollowGroup;
import edu.xhu.model.biliuser.pojos.BiliUser;
import edu.xhu.model.common.dtos.ResponseResult;
import edu.xhu.model.common.enums.AppHttpCodeEnum;
import edu.xhu.model.common.enums.IsFlagEnum;
import edu.xhu.utils.thread.BiliUserThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class BiliUserFollowServiceImpl extends ServiceImpl<BiliUserFollowMapper, BiliUserFollow> implements BiliUserFollowService {

    private final IdClient idClient;

    /**
     * 关注用户
     */
    @Override
    @Transactional
    public ResponseResult<?> follow(Long userId) {
        //1.参数校验
        if (userId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        BiliUserFollow biliUserFollow = new BiliUserFollow();
        BiliUser user = BiliUserThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SIG_TIMEOUT);
        }
        //存在记录,update
        BiliUserFollow record = getOne(Wrappers.<BiliUserFollow>lambdaQuery().eq(BiliUserFollow::getUserId, user.getUserId()).eq(BiliUserFollow::getFollowId, userId));
        if (record != null) {
            record.setIsFollow(IsFlagEnum.YES.getCode());
            if (!updateById(record)) {
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "更新失败！");
            }
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        //否则insert
        Long id = IdGen.getBehaviorId(idClient);
        if (id == 0L) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "id生成失败");
        }
        biliUserFollow.setId(id)
                .setUserId(user.getUserId())
                .setFollowId(userId)
                .setIsFollow(IsFlagEnum.YES.getCode())
                .setFollowType(FollowType.DEFAULT.getCode())
                .setGroupId(null)
                .setCreatedTime(new Date())
                .setVisitNumber(0);
        if (!save(biliUserFollow)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "关注失败！");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 取消关注
     */
    @Override
    @Transactional
    public ResponseResult<?> cancelFollow(Long userId) {
        //1.参数校验
        if (userId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        BiliUser user = BiliUserThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //更新record
        BiliUserFollow record = getOne(Wrappers.<BiliUserFollow>lambdaQuery().eq(BiliUserFollow::getFollowId, userId).eq(BiliUserFollow::getUserId, user.getUserId()));
        if (record == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        record.setIsFollow(IsFlagEnum.YES.getCode());
        record.setGroupId(null);
        record.setFollowType(FollowType.DEFAULT.getCode());
        if (!updateById(record)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "取消关注失败！");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询这个用户的所有粉丝
     */
    @Override
    public ResponseResult<?> allFans(Long userId) {
        if (userId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        List<BiliUserFollow> fans = list(Wrappers.<BiliUserFollow>lambdaQuery()
                .eq(BiliUserFollow::getFollowId, userId)
                .eq(BiliUserFollow::getIsFollow, IsFlagEnum.YES.getCode()));
        if (fans == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "粉丝查询失败！");
        }
        return ResponseResult.okResult(fans);
    }

    /**
     * 移除这个粉丝
     */
    @Override
    public ResponseResult<?> delFans(Long userId) {
        //1.参数校验
        if (userId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        BiliUser user = BiliUserThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        BiliUserFollow fans = getOne(Wrappers.<BiliUserFollow>lambdaQuery().eq(BiliUserFollow::getUserId, userId).eq(BiliUserFollow::getFollowId, user.getUserId()));
        if (fans == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //2.set,移除粉丝不会怎么的
        fans.setIsFollow(IsFlagEnum.NO.getCode());
        //3.update
        if (!updateById(fans)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "移除失败！");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
